<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Landlord extends My_Controller {

    function __construct() {
        parent::__construct();
    }

    public function index() {
        $this->navigation->loadPropertyManagementView();
    }

    public function propertymanagement() {
        $this->navigation->loadPropertyManagementView();
    }

    public function appraisal() {
        $this->load->model('au_suburb_geo_model', 'suburb');
        $this->load->model('franchise_model', 'franchise');
        $posts = $this->input->post();
        $data['post'] = $posts;
        $data['public_key'] = $this->config->item('public_key');
        $data['error'] = null;
        $this->load->helper('email');
        $this->load->helper('sendmail');
        if ($posts) {
            //var_dump($posts);die;
            $suburb_id = !empty($posts['address_suburb_id']) ? $posts['address_suburb_id'] : "";
            //$suburd_Geo = $this->suburb->getSuburbById($suburb_id);
            //var_dump($suburd_Geo);die;
            //$emailTo = !empty($suburd_Geo->email) ? $suburd_Geo->email : null;
             if (!empty($suburb_id)) {            
                $suburd_Geo = $this->suburb->getSuburbById($suburb_id);
                $suburbGeo = $this->suburb->getSuburbGeoById($suburb_id);
    //            if (!empty($suburbGeo)){}
    //                $data['office'] = $this->franchise->getOfficeNearest($suburbGeo->Lat, $suburbGeo->Long);
                if(!empty($suburd_Geo)){
                    $emailTo = $suburd_Geo->email;
                    //var_dump($suburd_Geo);die;
                }
                else{
                    if (!empty($suburbGeo)){
                        $data['office'] = $this->franchise->getOfficeNearest($suburbGeo->Lat, $suburbGeo->Long);
                        $emailTo = $data['office']->email;
                    }
                }
            }
            $privateKey = $this->config->item('private_key');
            $validationErrors = array();
            if ($posts['suburb'] == '') {
                $validationErrors['form-suburbs'] = "Suburb cannot be blank";
            }
            if ($posts['address'] == '') {
                $validationErrors['address-street'] = "Address cannot be blank";
            }
            if ($posts['name'] == '') {
                $validationErrors['name'] = "Your Name cannot be blank";
            }
            if ($posts['phone'] == '') {
                $validationErrors['phone'] = "Contact Number cannot be blank";
            }
            if ($posts['email'] == '') {
                $validationErrors['emailAddress'] = "Email cannot be blank";
            } elseif (!valid_email($posts['email'])) {
                $validationErrors['emailAddress'] = "Email is not valid";
            }

            $resp = recaptcha_check_answer($privateKey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
            if (!$resp->is_valid) {
                $validationErrors['recaptcha_response_field'] = $resp->error;
            }
            if (count($validationErrors) != 0) {
                $data['usermessage'] = array('error', 'Please complete all required fields');
                $data['fielderrors'] = $validationErrors;
            } else {
                $content = '<b>Suburb</b> :' . $posts['suburb'] . '<br>';
                $content .= '<b>Your Name </b> :' . $posts['name'] . '<br>';
                $content .= '<b>Contact Number</b> :' . $posts['phone'] . '<br>';
                $content .= '<b>Mobile</b>  :' . $posts['mobile'] . '<br>';
                $content .= '<b>Email</b>  :' . $posts['email'] . '<br>';
                $content .= '<b>Address</b>  :' . $posts['address'] . ', ' . $posts['address_suburb'] . '<br>';
                $content .= '<b>Type</b>  :' . $posts['propertyType'] . '<br>';
                $content .= '<b>Bedrooms</b>   :' . $posts['bedrooms'] . '<br>';
                $content .= '<b>Bathrooms</b>   :' . $posts['bathrooms'] . '<br>';
                $content .= '<b>How did you hear about us?</b>  :' . $posts['your_hear'] . ' ' . $posts['hear_other'] . '<br>';
                if (sendmail($emailTo, 'Free Market Appraisal - Rental', $content, $posts['email'], $posts['name'])) {
                    $data['usermessage'] = array('success', 'Thank you - Your email request is being sent and a Sales Agent will be in contact with you directly');
                    $data['post'] = '';
                } else {
                    $data['usermessage'] = array('error', 'Email sent Error');
                }
            }
        }
        $this->navigation->loadRentalAppraisalView($data);
    }

}

/* End of file landlord.php */
/* Location: ./application/controllers/landlord.php */